/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.knezevic.realestate.hibernate;

import com.knezevic.realestate.Advert;
import com.knezevic.realestate.beans.AdvertBean;
import com.knezevic.realestate.service.Adverts;
import java.io.Serializable;
import java.util.Collection;
import org.hibernate.Criteria;
import org.hibernate.Session;

/**
 *
 * @author Nemanja
 */
public class AdvertsServiceHibernate implements Adverts{

    private final Session session;

    public AdvertsServiceHibernate(Session session) {
        this.session = session;
    }

    
    @Override
    public Advert save(Advert advert) {
        return (Advert) session.get(AdvertBean.class, session.save(advert));
    }

    @Override
    public void update(Advert advert) {
        session.merge(advert);
//      session.saveOrUpdate(advert);
    }

    @Override
    public void deleteAdvert(Advert advert) {
        session.delete(advert);
    }

    @Override
    public Collection<Advert> retrieve() {
        Criteria criteria = session.createCriteria(AdvertBean.class);
        return criteria.list();
    }

    @Override
    public Advert retrieve(Serializable id) {
        return (Advert) session.get(AdvertBean.class, id);
    }
}
